package com.ruoyi.web.controller.iot;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.iot.service.IIotDeviceControlService;

/**
 * 设备控制接口
 */
@RestController
@RequestMapping("/iot/control")
public class IotDeviceControlController extends BaseController {
    @Autowired
    private IIotDeviceControlService deviceControlService;

    /**
     * 获取在线设备列表
     */
    @PreAuthorize("@ss.hasPermi('iot:device:list')")
    @GetMapping("/online")
    public AjaxResult getOnlineDevices() {
        List<Map<String, Object>> list = deviceControlService.getOnlineDevices();
        return success(list);
    }

    /**
     * 获取设备信息
     */
    @PreAuthorize("@ss.hasPermi('iot:device:query')")
    @GetMapping("/info/{deviceId}")
    public AjaxResult getDeviceInfo(@PathVariable("deviceId") String deviceId) {
        Map<String, Object> info = deviceControlService.getDeviceInfo(deviceId);
        return success(info);
    }

    /**
     * 运行脚本
     */
    @PreAuthorize("@ss.hasPermi('iot:device:control')")
    @Log(title = "设备控制", businessType = BusinessType.OTHER)
    @PostMapping("/script/run/{deviceId}")
    public AjaxResult runScript(@PathVariable("deviceId") String deviceId, @RequestBody Map<String, String> params) {
        String scriptName = params.get("name");
        String scriptCode = params.get("code");
        deviceControlService.runScript(deviceId, scriptName, scriptCode);
        return success();
    }

    /**
     * 停止脚本
     */
    @PreAuthorize("@ss.hasPermi('iot:device:control')")
    @Log(title = "设备控制", businessType = BusinessType.OTHER)
    @PostMapping("/script/stop/{deviceId}")
    public AjaxResult stopScript(@PathVariable("deviceId") String deviceId) {
        deviceControlService.stopScript(deviceId);
        return success();
    }

    /**
     * 获取设备截图
     */
    @PreAuthorize("@ss.hasPermi('iot:device:control')")
    @GetMapping("/screenshot/{deviceId}")
    public AjaxResult getScreenshot(@PathVariable("deviceId") String deviceId) {
        deviceControlService.getScreenshot(deviceId);
        return success();
    }
} 